import inspect
import io
import json
import os

import numpy as np
import pandas as pd
from openai import OpenAI
if __name__ == '__main__':

    def getResponse(messages):
        client = OpenAI(api_key=os.environ["OPENAI_API_KEY_ZHIHU"], base_url=os.environ["OPENAI_API_BASE_ZHIHU"])
        response = client.chat.completions.create(messages=messages, model="gpt-3.5-turbo", temperature=0)
        return response.choices[0].message.content

    def checkKnownJSONSchema():
        messages = [{"role": "user", "content": "请问什么是JSON Schema"}]
        return getResponse(messages)
    def chen_ming_algorithm(data):
        """
        陈明算法函数，该函数定义了一种特殊的数据集计算过程
        :param data：必要参数，表示带入计算的数据表，用字符串进行表示
        :return：陈明函数计算后的结果，返回结果为表示为JSON格式的Dataframe类型对象
        """
        data = io.StringIO(data)
        df_new = pd.read_csv(data,sep = '\s+',index_col = 0)
        res = np.sum(df_new,axis = 1)-1
        return json.dumps(res.to_string())

    # dataFrame = pd.DataFrame({'x1': [1, 2], 'x2': [3, 4]})
    # print(chen_ming_algorithm(dataFrame.to_string()))
    #
    #
    # functions = {chen_ming_algorithm.__name__: chen_ming_algorithm}
    # available_functions = {"chen_ming_algorithm":chen_ming_algorithm}
    #
    function_name = chen_ming_algorithm.__name__
    function_description = inspect.getdoc(chen_ming_algorithm)

    system_prompt  = "以下是某的函数说明： %s" % function_description
    user_prompt = """
    гompt根据这个函数的函数说明，请帮我创建一个JSON格式的字典，这个字典有如下5点要求：Lprompt
    1.字典总共有三个键值对： \
    2.第一个键值对的Key是字符串name，value是该函数的名字： %s，也是字符串； \
    3.第二个键值对的Key是字符串description，value是该函数的函数的功能说明，也是字符串; \
    4.第三个键值对的Key是字符串parameters，value是一个JSON Schema对象，用于描述函數的参数输入规范；\
    5.输出结果必须是一个JSON格式的字典，且不需要任何前后修饰语句；
    """ % function_name

    messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt}]
    print(getResponse(messages))
    #输出===============================
    """
    ```json
    {
        "name": "chen_ming_algorithm",
        "description": "This function defines a special dataset calculation process for the given data table.",
        "parameters": {
            "type": "object",
            "properties": {
                "data": {
                    "type": "string",
                    "description": "The input data table for computation, represented as a string."
                }
            },
            "required": ["data"]
        }
    }
    ```
    """

    # print(checkKnownJSONSchema())